package cn.edu.lingnan.dao;

import cn.edu.lingnan.pojo.Club;
import cn.edu.lingnan.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClubDaoMysqlImpl implements ClubDao{

    @Override
    public boolean updateClub(Club _club) {
        boolean flag=false;
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("update club set cname=?,cflag=? where cid=?");
            prep.setString(1,_club.getCname());
            prep.setString(2, _club.getCflag());
            prep.setString(3,_club.getCid());
            prep.executeUpdate();
            flag=true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(prep,conn);
        }
        return flag;
    }

    @Override
    public boolean newclub(Club _club) {
        boolean flag=false;
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("insert into club values(?,?,?)");
            prep.setString(1,_club.getCid());
            prep.setString(2,_club.getCname());
            prep.setString(3, _club.getCflag());
            prep.executeUpdate();
            flag=true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(prep,conn);
        }
        return flag;
    }

    @Override
    public List<Club> findClubAll() {
        List<Club> list = new ArrayList<>();
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            st = conn.createStatement();
            rs = st.executeQuery("select * from club");
            while (rs.next()) {
                Club club=new Club();
                club.setCid(rs.getString(1));
                club.setCname(rs.getString(2));
                club.setCflag(rs.getString(3));
                list.add(club);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(rs,st,conn);
        }
        return list;
    }

    @Override
    public Club findClubByCid(String _cid) {
        Club club=new Club();
        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("select * from club where cid=?");
            prep.setString(1,_cid);
            rs = prep.executeQuery();
            while (rs.next()) {
                club.setCid(rs.getString(1));
                club.setCname(rs.getString(2));
                club.setCflag(rs.getString(3));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(rs,prep,conn);
        }
        return club;
    }


    @Override
    public List<Club> findClubByCname(String _cname) {
        List<Club> list=new ArrayList<>();
        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("select * from club where cname like ?");
            prep.setString(1,"%" + _cname + "%");
            rs = prep.executeQuery();
            while (rs.next()) {
                Club club=new Club();
                club.setCid(rs.getString(1));
                club.setCname(rs.getString(2));
                club.setCflag(rs.getString(3));
                list.add(club);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(rs,prep,conn);
        }
        return list;
    }


    @Override
    public boolean updateCnameByCid(String _cid, String _cname) {
        boolean flag=false;
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("update club set cname=? where cid=?");
            prep.setString(1,"'" + _cname+"'");
            prep.setString(2,_cid);
            if(prep.executeUpdate()==1){
                flag=true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(prep,conn);
        }
        return flag;
    }


    @Override
    public boolean updateCflagByCid(String _cid, String _cflag) {
        boolean flag = false;
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("update club set cflag=? where cid=?");
            prep.setString(1, _cflag);
            prep.setString(2, _cid);
            prep.executeUpdate();
            if(prep.executeUpdate()==1){
                flag=true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(prep, conn);
        }
        return flag;
    }


    @Override
    public boolean deleteClubByCid(String _cid) {
        boolean flag=false;
        Connection conn = null;
        PreparedStatement prep = null;
        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("delete from club where cid =?");
            prep.setString(1,_cid);
            if(prep.executeUpdate()==1){
                flag=true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(prep,conn);
        }
        return flag;
    }
}
